package com.hairline.hairlinemarket.controller;

import com.hairline.hairlinemarket.bean.MarketUser;
import com.hairline.hairlinemarket.bean.common.BaseRespVo;
import com.hairline.hairlinemarket.bean.data.LoginUserData;
import com.hairline.hairlinemarket.bean.data.UserInfo;
import com.hairline.hairlinemarket.shiro.WdToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Author:杨小凯
 * Date:2023-04-19 18:38
 * Description:<描述>
 */
@RestController
@RequestMapping("auth")
public class AuthController {

    /*
    账号登录
     */
    @RequestMapping("/login")
    public BaseRespVo login(@RequestBody Map map){
        // 登录业务代码
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        WdToken token = new WdToken(username, password, WdToken.WX_TYPE);

        Subject subject = SecurityUtils.getSubject();
        subject.login(token);

        MarketUser principal = (MarketUser) subject.getPrincipal();
        // 通过Principal获得admininfo


        // 微信用户
        LoginUserData data = new LoginUserData();
        String id = (String) subject.getSession().getId();
        data.setToken(id);
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(principal.getNickname());
        userInfo.setAvatarUrl(principal.getAvatar());
        data.setUserInfo(userInfo);
        return BaseRespVo.ok(data);
    }

    /*
    微信登录
     */
    @RequestMapping("/login_by_weixin")
    public void loginByWX(@RequestBody Map map){
        // 登录业务代码
        System.out.println(map);
    }

    /*
    注册
     */
    @RequestMapping("/register")
    public void register(@RequestBody Map map){
        // 注册业务代码
        System.out.println(map);
    }


    /*
    验证码
     */
    @RequestMapping("/regCaptcha")
    public void regCaptcha(@RequestBody Map map){
        // 注册业务代码
        System.out.println(map);
    }


    /*
    密码重置
     */
    @RequestMapping("/reset")
    public void reset(@RequestBody Map map){
        // 注册业务代码
        System.out.println(map);
    }


    /*
    退出
     */
    @RequestMapping("/logout")
    public BaseRespVo logout(){

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

}
